package z;

import java.util.Scanner;

public class OrderingMgr {
        String[] names = new String[10];
        String[] dishMegs = new String[10];
        int[] times = new int[10];
        String[] addresses = new String[10];
        int[] states = new int[10];
        double[] sumPrices = new double[10];

        String[] dishNames = {"红烧肉", "烤鱼", "麻辣香锅"};
        double[] Prices = {5, 25, 15};
        int[] praiseNums = new int[dishNames.length];

        // 创建Scanner对象作为成员变量，整个类中共享使用
        Scanner input = new Scanner(System.in);


        public static void main(String[] args) {
            OrderingMgr order = new OrderingMgr();
            while (true) {
                order.showMainMenu();
                int choice = order.input.nextInt();
                switch (choice) {
                    case 1:
                        System.out.println("***我要订餐***");
                        order.placeOrder();
                        break;
                    case 2:
                        System.out.println("***查看餐袋***");
                        order.showOrders();
                        break;
                    case 3:
                        System.out.println("***签收订单***");
                        order.signOrder();
                        break;
                    case 4:
                        System.out.println("***删除订单***");
                        order.deleteOrder();
                        break;
                    case 5:
                        System.out.println("***我要点赞***");
                        order.praiseDish();
                        break;
                    case 6:
                        System.out.println("感谢使用外卖订餐系统，再见！");
                        order.input.close();
                        System.exit(0);
                        break;
                    default:
                        System.out.println("输入错误，请选择1-6之间的数字！");
                        break;
                }
            }
        }


        public void showMainMenu() {
            System.out.println("欢迎使用\"外卖订餐系统\"");
            System.out.println("*****************************");
            System.out.println("1.我要订餐");
            System.out.println("2.查看餐袋");
            System.out.println("3.签收订单");
            System.out.println("4.删除订单");
            System.out.println("5.我要点赞");
            System.out.println("6.退出系统");
            System.out.println("*****************************");
            System.out.print("请选择：");
        }


        public void placeOrder() {
            boolean flag = false;
            for (int j = 0; j < names.length; j++) {
                if (names[j] == null) {
                        flag = true;
                    System.out.print("请输入订餐人姓名：");
                    String name = input.next();


                    System.out.println("序号\t菜名\t单价\t点赞数");
                    for (int i = 0; i < dishNames.length; i++) {

                        String praiseNum = (praiseNums[i] > 0) ? praiseNums[i] + "赞" : "";
                        System.out.println((i + 1) + "\t" + dishNames[i] + "\t" + Prices[i] + "元\t" + praiseNum);
                    }


                    System.out.print("请选择菜品序号：");
                    int dishChoice = input.nextInt() - 1;

                    if (dishChoice < 0 || dishChoice >= dishNames.length) {
                        System.out.println("菜品选择无效！");
                        return;
                    }

                    System.out.print("请输入份数：");
                    int quantity = input.nextInt();
                    if (quantity <= 0) {
                        System.out.println("份数必须大于0！");
                        return;
                    }


                    System.out.print("请输入送餐时间(11-20点)：");
                    int time = input.nextInt();
                    if (time < 11 || time > 20) {
                        System.out.println("送餐时间必须在11-20点之间！");
                        return;
                    }

                    System.out.print("请输入送餐地址：");
                    String address = input.next();


                    double totalPrice = Prices[dishChoice] * quantity;
                    double deliveryFee = (totalPrice < 50) ? 5 : 0;
                    totalPrice += deliveryFee;  // 加上配送费


                    names[j] = name;
                    dishMegs[j] = dishNames[dishChoice] + " " + quantity + "份";
                    times[j] = time;
                    addresses[j] = address;
                    states[j] = 0;
                    sumPrices[j] = totalPrice;


                    System.out.println("订餐成功！");
                    System.out.println("订单详情：");
                    System.out.println("订餐人：" + name);
                    System.out.println("菜品：" + dishMegs[j]);
                    System.out.println("送餐时间：" + time + "点");
                    System.out.println("送餐地址：" + address);
                    System.out.println("总金额：" + totalPrice + "元" +
                            (deliveryFee > 0 ? "（含配送费5元）" : ""));
                    break;
                }
            }

            if (!flag) {
                System.out.println("订单已满，无法订餐！");
            }
        }


        public void showOrders() {
            boolean hasOrder = false;
            System.out.println("序号\t订餐人\t餐品信息\t送餐时间\t送餐地址\t总金额\t状态");
            for (int i = 0; i < names.length; i++) {
                if (names[i] != null) {
                        hasOrder = true;

                    String state = (states[i] == 0) ? "已预定" : "已完成";

                    System.out.println((i + 1) + "\t" + names[i] + "\t" + dishMegs[i] + "\t" +
                            times[i] + "点\t" + addresses[i] + "\t" + sumPrices[i] + "元\t" + state);
                }
            }
            if (!hasOrder) {
                System.out.println("暂无订单！");
            }
        }


        public void signOrder() {
            showOrders();
            System.out.print("请选择要签收的订单序号：");
            int orderNum = input.nextInt() - 1;


            if (orderNum < 0 || orderNum >= names.length || names[orderNum] == null) {
                System.out.println("订单不存在！");
                return;
            }

            if (states[orderNum] == 1) {
                System.out.println("该订单已完成签收！");
            } else {
                states[orderNum] = 1;
                System.out.println("订单签收成功！");
            }
        }


        public void deleteOrder() {
            showOrders();
            System.out.print("请选择要删除的订单序号：");
            int orderNum = input.nextInt() - 1;

            if (orderNum < 0 || orderNum >= names.length || names[orderNum] == null) {
                System.out.println("订单不存在！");
                return;
            }


            if (states[orderNum] == 0) {
                System.out.println("该订单未完成，不能删除！");
                return;
            }


            names[orderNum] = null;
            dishMegs[orderNum] = null;
            addresses[orderNum] = null;
            times[orderNum] = 0;
            states[orderNum] = 0;
            sumPrices[orderNum] = 0;

            System.out.println("订单删除成功！");
        }


        public void praiseDish() {

            System.out.println("序号\t菜名\t单价\t点赞数");
            for (int i = 0; i < dishNames.length; i++) {
                String praiseNum = (praiseNums[i] > 0) ? praiseNums[i] + "赞" : "";
                System.out.println((i + 1) + "\t" + dishNames[i] + "\t" + Prices[i] + "元\t" + praiseNum);
            }

            System.out.print("请选择要点赞的菜品序号：");
            int dishChoice = input.nextInt() - 1;


            if (dishChoice < 0 || dishChoice >= dishNames.length) {
                System.out.println("菜品选择无效！");
                return;
            }

            praiseNums[dishChoice]++;
            System.out.println("点赞成功！" + dishNames[dishChoice] + "当前点赞数：" + praiseNums[dishChoice]);
        }

    }
